﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using XinYi.CrookedShow.BLL;
using XinYi.CrookedShowBack.Models;
using XinYi.CrookedShowMode;

namespace XinYi.CrookedShowBack.Controllers
{
    public class BrandController : Controller
    {
        // GET: Brand
        BrandService brandService = new BrandService();
        #region 查询分页
        public JsonResult GetBrandPageList(int page, int limit, string purName,string start)
        {
            DateTime dateTime = new DateTime();
            if (!string.IsNullOrEmpty(start))
            {
                dateTime = Convert.ToDateTime(start);
            }
            PageListResult<BrandInfo> pageListResult = new PageListResult<BrandInfo>();
            List<BrandInfo> roleUserQueries = brandService.GetRoleList();
            if (!string.IsNullOrEmpty(purName) && !string.IsNullOrEmpty(start))
            {
                roleUserQueries=roleUserQueries.Where(a => a.Name.Contains(purName) && a.UpdateTime == dateTime).ToList();
            }
            else if (!string.IsNullOrEmpty(purName))
            {
                roleUserQueries=roleUserQueries.Where(a => a.Name.Contains(purName)).ToList();
            }
            else if (!string.IsNullOrEmpty(start))
            {
                roleUserQueries=roleUserQueries.Where(a => a.UpdateTime == dateTime).ToList();
            }
            pageListResult.data = roleUserQueries.Skip((page - 1) * limit).Take(limit).ToList();
            pageListResult.msg = string.Empty;
            pageListResult.code = 0;
            pageListResult.count = roleUserQueries.Count;
            return Json(pageListResult, JsonRequestBehavior.AllowGet);
        }
        #endregion

        #region 添加或修改品牌
        public JsonResult AddUpdateBrand(Brand brand)
        {
            //创建时间
            brand.UpdateTime = DateTime.Now;
            Operate operate = new Operate();
            if (brand.Id != 0)
            {
                //修改
                operate.Success = brandService.Update(brand);
            }
            else
            {
                //添加              
                operate.Success = brandService.Add(brand);
            }
            return Json(operate);
        }
        #endregion

        #region 删除品牌，和品牌下的商品
        public JsonResult DelBrand(Brand brand)
        {
            Operate operate = new Operate();
            operate.Success = brandService.DelBrand(brand);
            return Json(operate);
        }
        #endregion

        #region 是否上架，修改
        public JsonResult UpdateStatus(Brand brand)
        {
            Operate operate = new Operate();
            operate.Success = brandService.Update(brand);
            return Json(operate);
        }
        #endregion

        #region 获取所有品牌
        public JsonResult GetBrandToList()
        {
            List<Brand> brands = brandService.QueryList(a => true);
            return Json(brands);
        }
        #endregion
    }
}